#include "../include/AuthManager.hh"
#include "../include/Customer.hh"
#include "../include/Employee.hh"
#include "../include/Admin.hh"
#include "../include/Auditor.hh"
#include <iostream>

AuthManager::AuthManager() : currentUser(nullptr) {
}

bool AuthManager::authenticateAsCustomer(const std::string& username, const std::string& password) {
    // 创建临时客户对象进行密码验证
    auto tempCustomer = std::make_shared<Customer>(username, password);
    
    // 客户需要密码验证
    if (password.empty()) {
        std::cout << "错误：客户认证失败，需要密码。" << std::endl;
        return false;
    }
    
    // 实际应用中应该查找注册的客户并验证密码
    // 但这里我们简单地设置currentUser
    currentUser = std::make_shared<Customer>(username, password);
    return true;
}

bool AuthManager::authenticateAsEmployee(const std::string& username, const std::string& secretKey) {
    // 验证秘钥
    if (!validateSecretKey(secretKey)) {
        std::cout << "错误：员工认证失败，秘钥无效。" << std::endl;
        return false;
    }
    
    currentUser = std::make_shared<Employee>(username);
    return true;
}

bool AuthManager::authenticateAsAdmin(const std::string& username, const std::string& secretKey) {
    // 验证秘钥
    if (!validateSecretKey(secretKey)) {
        std::cout << "错误：管理员认证失败，秘钥无效。" << std::endl;
        return false;
    }
    
    currentUser = std::make_shared<Admin>(username);
    return true;
}

bool AuthManager::authenticateAsAuditor(const std::string& username, const std::string& secretKey) {
    // 验证秘钥
    if (!validateSecretKey(secretKey)) {
        std::cout << "错误：审计员认证失败，秘钥无效。" << std::endl;
        return false;
    }
    
    currentUser = std::make_shared<Auditor>(username);
    return true;
}

bool AuthManager::validateSecretKey(const std::string& inputKey) const {
    return (inputKey == SECRET_KEY);
}

std::shared_ptr<User> AuthManager::getCurrentUser() const {
    return currentUser;
}

Role AuthManager::getCurrentUserRole() const {
    if (currentUser) {
        return currentUser->getRole();
    }
    
    // 默认返回客户角色
    return Role::CUSTOMER;
}

bool AuthManager::isAuthenticated() const {
    return (currentUser != nullptr);
}

void AuthManager::logout() {
    currentUser = nullptr;
} 